package com.omt.tarjimdinek.domain.user.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;

import com.google.common.base.Objects;
import com.omt.tarjimdinek.domain.islamicLibrary.entity.Sahabi;
import com.omt.tarjimdinek.support.Identifiable;

/**
 * roles of user
 * @author chabane
 *
 */
@Entity
@Table(name="a01_role")
public class Role implements Identifiable<Long>, Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(Role.class);
    
    public static final String ROLE_MEMBRE = "ROLE_MEMBRE";
    public static final String ROLE_CORRECTEUR = "ROLE_CORRECTEUR"; 
    public static final String ROLE_TRADUCTEUR = "ROLE_TRADUCTEUR"; 
    public static final String ROLE_VALIDATEUR = "ROLE_VALIDATEUR"; 
    public static final String ROLE_MODERATEUR = "ROLE_MODERATEUR";
    public static final String ROLE_ADMINISTRATEUR = "ROLE_ADMINISTRATEUR";
    public static final String ROLE_JURE = "ROLE_JURE";
    
	/** ID SGBD */
	@Column(name="CNROLE", length=11)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	/** User */
	@JoinColumn(name="CNUSR", nullable=false, unique=true)
	@ManyToOne
	private Utilisateur utilisateur;

	/** label */
	@Size(max = 45)
	@NotEmpty
	@Column(name="LIROLE", length=45, nullable=false)
	private String libelle;

	/** List of authorizations */
	@JoinTable(name = "a01_role_hbt", 
			joinColumns = @JoinColumn(name = "CNROLE"), 
			inverseJoinColumns = @JoinColumn(name = "CNHBT"))
	@ManyToMany
	private List<Habilitation> listeHabilitations;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the utilisateur
	 */
	public Utilisateur getUtilisateur() {
		return utilisateur;
	}

	/**
	 * @param utilisateur the utilisateur to set
	 */
	public void setUtilisateur(Utilisateur utilisateur) {
		this.utilisateur = utilisateur;
	}

	public String getLibelle() {
		return libelle;
	}

	public void setLibelle(String libelle) {
		this.libelle = libelle;
	}
	
	public List<Habilitation> getListeHabilitations() {
		return listeHabilitations;
	}

	public void setListeHabilitations(List<Habilitation> listeHabilitations) {
		this.listeHabilitations = listeHabilitations;
	}

	@Transient
	public boolean isIdSet() {
		return id != null && id.compareTo(Long.valueOf(0)) != 0;
	}
	
	/**
     * equals implementation using a business key.
     */
    public boolean equals(Object other) {
        return this == other || (other instanceof Sahabi && hashCode() == other.hashCode());
    }

    public int hashCode() {
        int hashCode = Objects.hashCode(getId());
        return hashCode;
    }
    
    /**
     * Construct a readable string representation for this instance.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return Objects.toStringHelper(this) //
                .add("id", getId()) //
                .add("login", getUtilisateur()) //
                .add("libelle", getLibelle()) //
                .add("listeHabilitations", getListeHabilitations()) //
                .toString();
    }

	public void initDefaultValues() {
		// TODO Auto-generated method stub
		
	}
}
